#include "Vector.h"
#include <math.h>
#include <stdlib.h>  /* for abort() */

//For debugging pruposes only!
#include <iostream>

Vector::Vector(){
	x = 0;
	y = 0;
	z = 0;
	homogenous = 0;
}

Vector::Vector(const Vector& other) 
    :x(other.x), y(other.y), z(other.z), homogenous(other.homogenous) {
}

double& Vector::operator[](int index){
	if(index < 0 || index > 3){
		std::cerr << "Trying to access out-of-bounds vector index" << std::endl;
		abort();
	}
	else{
		switch(index){
			case 0:
				return x;
			case 1:
				return y;
			case 2:
				return z;
			case 3:
				return homogenous;
		}
	}
	return homogenous;// Just to shut up the compiler, this will never be reached
}

double Vector::magnitude(){
	return sqrt(pow(this->x, 2) + pow(this->y, 2) + pow(this->z, 2));
}

double Vector::dotProduct(const Vector& v2){
	return this->x * v2.x + this->y * v2.y + this->z * v2.z;
}

Vector Vector::cross(const Vector& v2){
	return Vector(this->y*v2.z - this->z*v2.y, this->z*v2.x - this->x*v2.z, this->x*v2.y - this->y*v2.x, 0);
}

void Vector::normalize(){
	double m = this->magnitude();
	x = x/m;
	y = y/m;
	z = z/m;
}

